<?php

namespace App\Console\Commands;

use App\Models\GoldCash;
use App\Models\GoldEnd;
use App\Models\GoldRound;
use App\Models\GoldSeed;
use App\Models\GoldBean;
use App\Models\GoldCoin;
use App\Models\GoldCharge;
use App\Models\GoldRecord;
use App\Models\GoldProduct;
use App\Models\GoldSplit;
use App\Models\GoldSubsidy;
use Illuminate\Console\Command;
use App\Services\RewardGoldPrize\RewardGoldPrize;
use Illuminate\Support\Facades\Redis;

class TestStormReturn extends Command
{
    protected $signature = 'cc:test-storm-return';

    protected $description = "";

    public function handle()
    {
        //if (app()->environment('production')) return;

        $this->backUsers();
        $this->backGoldRecords();
        $this->backGoldProducts();
        app(RewardGoldPrize::class)();
        GoldRound::initCache();
    }

    protected function backUsers()
    {
        GoldCharge::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });

        GoldCoin::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });

        GoldBean::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });

        GoldCash::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });

        GoldEnd::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });

        GoldSplit::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });

        GoldSubsidy::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });
    }

    protected function backGoldRecords()
    {
        GoldRecord::query()->chunks(50, function (\Illuminate\Support\Collection $records) {

            if ($records->isEmpty()) return false;

            /**
             * @var GoldRecord $record
             */
            foreach ($records as $record) {
                $user = $record->user;
                $cacheId = $record->cacheKey();

                $isInPendingCache = $user->isRecordInPendingCache($cacheId);
                $isInRunningCache = $user->isRecordInRunningCache($cacheId);
                $isInFinishedCache = $user->isRecordInFinishedCache($cacheId);

                $user->delRecordFromPendingCache($cacheId);
                $user->delRecordFromRunningCache($cacheId);
                $user->delRecordFromFinishedCache($cacheId);

                $user->delMyRecordCache($cacheId);
                
                Redis::del($user->voucherRecordsCacheKey($record->cache_id));
                if($record->cache_id){

                    $cache = explode("_",$record->cache_id);
                    $cache[1] = date("Ymd",strtotime($record->created_at->addDays(-1)->toDateString()));
                    $cacheId = implode("_",$cache);
                }


                $record->update([
                    'paid_at' => $record->paid_at ? $record->paid_at->addDays(-1) : null,
                    'to_resell_applied_at' => $record->to_resell_applied_at ? $record->to_resell_applied_at->addDays(-1) : null,
                    'started_at' => $record->started_at ? date("Y-m-d H:i:s",(strtotime($record->started_at)-86400)) : null,
                    'to_split_applied_at' => $record->to_split_applied_at ? $record->to_split_applied_at->addDays(-1) : null,
                    'to_take_applied_at' => $record->to_take_applied_at ? $record->to_take_applied_at->addDays(-1) : null,
                    'created_at' => $record->created_at->addDays(-1),
                    'updated_at' => $record->updated_at->addDays(-1),
                    'cache_id' => $cacheId,
                ]);

                $record = $record->fresh();
                if ($isInPendingCache) {
                    $user->syncRecordToPendingCache($record);
                }
                if ($isInRunningCache) {
                    $user->syncRecordToRunningCache($record);
                }
                if ($isInFinishedCache) {
                    $user->syncRecordToFinishedCache($record);
                }
            }

            return true;
        });
    }

    protected function backGoldProducts()
    {
        GoldProduct::all()->each(function ($item) {
            $item->update([
                'started_at' => $item->started_at ? $item->started_at->addDays(-1) : null,
                'disabled_at' => $item->disabled_at ? $item->disabled_at->addDays(-1) : null,
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });
        GoldSeed::all()->each(function ($item) {
            $item->update([
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });
        GoldEnd::all()->each(function ($item) {
            $item->update([
                'ended_at' => $item->ended_at? $item->ended_at->addDays(-1) : null,
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });
        GoldSplit::all()->each(function ($item) {
            $item->update([
                'started_at' => $item->started_at? $item->started_at->addDays(-1) : null,
                'settled_at' => $item->settled_at? $item->settled_at->addDays(-1) : null,
                'created_at' => $item->created_at->addDays(-1),
                'updated_at' => $item->updated_at->addDays(-1),
            ]);
        });
    }
}
